package io.springboot.jpa.examples.springbootjpaexamples.config.shiro;

import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.ShiroWebFilterConfiguration;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yzc
 * @description
 * @date 2018/9/7
 */
@Configuration
public class ShiroConfiguration extends ShiroWebFilterConfiguration {

    /**
     * 定义Realm
     */
    @Bean
    public Realm realm() {
        return new StatelessRealm();
    }

    @Bean
    @Override
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(this.securityManager);
        Map<String, Filter> filters = new HashMap<>();
        filters.put("jwt", new StatelessFilter());
        filters.put("anon", new AllowCrossDomainAnonymousFilter());
        shiroFilterFactoryBean.setFilters(filters);
        Map<String, String> filterChain = new HashMap<>();
        filterChain.put("/**", "jwt");
        filterChain.put("/auth", "anon");
        filterChain.put("/druid/**", "anon");
        filterChain.put("/actuator/**", "anon");
        filterChain.put("/favicon.ico", "anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChain);
        return shiroFilterFactoryBean;
    }


    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }
}
